const express = require('express')
const cors = require('cors')
const router = require('./router/user')
const kunrouter = require('./router/kun')
const joi = require('joi')
const config = require('./config')
const expressJWt = require('express-jwt')
const app = express()
app.use(cors())
app.use(express.urlencoded({ extended: false }))
app.use(express.json());
app.use(expressJWt({ secret: config.jsonKey }).unless({ path: /^\/api|kun/ }))
app.use((req, res, next) => {
    res.cc = (err, status = 1) => {
        res.send({
            status,
            message: err instanceof Error ? err.message : err
        })
    }
    next()
})
app.use('/api', router)
app.use('/kun', kunrouter)
app.use((err, req, res, next) => {
    if (err instanceof joi.ValidationError) return res.cc(err)
    if (err.name === 'UnauthorizedError') return res.send({ message: '身份认证失败' })
    res.cc(err)
})
app.listen(90, () => {
    console.log('启动成功');
})